package com.tianqu.tms.www.transfer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransMarketCode;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class TransferSeniorSearchDetailAction extends TmsAction {
	private Logger logger = Logger.getLogger(TransferSeniorSearchDetailAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String serial = "";
		if(request.getParameter("serial")!=null)
		{
			serial = request.getParameter("serial");
		}else{
			serial = (String)uc.getAttribute("serial");
		}
		String tishixinxi = (String)uc.getAttribute("tishixinxi");		
		uc.removeAttribute("tishixinxi");
		//根据流水号查询所有车辆信息
		TransferRgInputViewHelper view = new TransferRgInputViewHelper();
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		accept = accept.getAcceptInfoBySerial(serial,"");
		view.setSerial(serial);
		view.setTishixinxi(tishixinxi);
		Car car = new Car();
		Codes code = Codes.getInstance();
		TransferPersonalCarInfo person = new TransferPersonalCarInfo();
		if(accept.getMarketType() == 1)
		{
			//库存
			car = car.findCarBySerial(accept.getBasicCarId());
			view = toViewHelper(car, view);
		}
		if(accept.getMarketType() == 2)
		{
			//非库存
			person = person.getPersonalCarInfoBySerial(accept.getBasicCarId());
			view = toViewHelper1(person, view);
		}
		/******过户信息部分 */
		view.setVin(accept.getVin());
		view.setChangeTradeTicket(accept.getChangeTradeTicket());//是否变更交易票
		view.setStatus(String.valueOf(accept.getStatus()));
		view.setStatusName(code.getItemValueByItemIdAndItemCode(Codes.ACCEPT_STATUS,accept.getStatus()));
		view.setMaterialImages1(accept.getMaterialImages1());
		view.setMaterialImages2(accept.getMaterialImages2());
		view.setMaterialImages3(accept.getMaterialImages3());
		view.setMaterialImages4(accept.getMaterialImages4());
		view.setMaterialImages5(accept.getMaterialImages5());
		view.setOwnersGroupPhoto(accept.getOwnersGroupPhoto());
		view.setWaitForStatus(String.valueOf(accept.getWaitForStatus()));
		
		view.setPhoto_1(accept.getPhoto_1());
		view.setPhoto_2(accept.getPhoto_2());
		view.setPhoto_3(accept.getPhoto_3());
		view.setPhoto_4(accept.getPhoto_4());
		
		view.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER,accept.getOwnerType()));
		view.setCarOwner(accept.getCarOwner());
		if(accept.getCy_cardType()!=null && !"".equals(accept.getCy_cardType()))
		{
			view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, Integer.parseInt(accept.getCy_cardType())));
		}else{
			view.setCardType("");
		}
		
		
		view.setTransferType(code.getItemValueByItemIdAndItemCode(Codes.TRANSFER_TYPE,accept.getTransferType()));
		view.setCarMarketIn(accept.getCarMarketIn());
		view.setMoveOutProvince(accept.getMoveOutProvince());
		view.setMoveOutCity(accept.getMoveOutCity());
		view.setGainType(code.getItemValueByItemIdAndItemCode(Codes.GAIN_TYPE, accept.getGainType()));
		view.setNewOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER, accept.getNewOwnerType()));
		view.setNewCarOwner(accept.getNewcarOwner());
		view.setNewcarOwnerIdCardNo(accept.getNewcarOwnerIdCardNo());
		view.setNewOwnerOrgNo(accept.getNewOwnerOrgNo());
		view.setNewOwnerAddress(accept.getNewOwnerAddress());
		view.setNewOwnerPhone(accept.getNewOwnerPhone());
		view.setNewOwnerTelPhone(accept.getNewOwnerTelPhone());
		view.setZipCode(accept.getNewOwnerZipCode());
		view.setMailAddress(accept.getNewOwnerMailAddress());
		view.setNewAgentName(accept.getNewAgentName());
		view.setNewAgentOwnerIdCardNo(accept.getNewAgentOwnerIdCardNo());
		view.setNewAgentAddress(accept.getNewAgentAddress());
		view.setNewAgentPhone(accept.getNewAgentPhone());
		view.setAgentName(accept.getAgentName());
		view.setAgentPhone(accept.getAgentPhone());
		view.setAgentAddress(accept.getAgentAddress());
		view.setAgentOwnerIdCardNo(accept.getAgentOwnerIdCardNo());
		if(accept.getTransferPrice() > 0)
		{
			view.setTransferPrice(StringUtil.addDH(String.valueOf(accept.getTransferPrice()))); 
		}else{
			view.setTransferPrice(""); 
		}
		if(accept.getPrice() > 0)
		{
			view.setPrice(StringUtil.addDH(String.valueOf(accept.getPrice()))); 
		}else{
			view.setPrice(""); 
		}
		view.setTaxNo(accept.getTaxNo());
		view.setRemark(accept.getRemark());
		view.setAttnId(accept.getAttnId());
		Attn attn = new Attn();
		if(accept.getAttnId()!=null && !"".equals(accept.getAttnId())) 
		{
			attn = attn.findAttnByAttnId(accept.getAttnId());
			if(attn!=null)
			{
				view.setAttnName(attn.getAttnName());
				view.setAttnPhoto(attn.getAttnPhoto());
			}else{
				view.setAttnName("");
				view.setAttnPhoto("");
			}
		}else{
			view.setAttnName("");
			view.setAttnPhoto("");
		}
		view.setTransNo(accept.getTransNo());
		if(accept.getTransMarket()!=null && !"".equals(accept.getTransMarket()))
		{
			TransMarketCode transMarket = new TransMarketCode();
			transMarket = transMarket.getTransMarketCodeInfo(accept.getTransMarket());
			if(transMarket!=null)
			{
				view.setTransMarket(transMarket.getMarketName());
			}else{
				view.setTransMarket("");
			}
		}else{
			view.setTransMarket("");
		}
		
		/**
		 * 页面按钮显示辨识符
		 */
		if(accept.getLc_printNum() > 0)
		{
			//liu cheng 
			view.setBs1("1");
		}
		if(accept.getCy_printNum() > 0)
		{
			//cha yan
			view.setBs2("1");
		}
		if(accept.getPg_printNum() > 0)
		{
			//ping gu
			view.setBs3("1");
		}
		if(accept.getDb_printNum() > 0)
		{
			//dai ban
			view.setBs4("1");
		}
		if(accept.getZy_printnum() > 0)
		{
			//zhuan yi
			view.setBs5("1");
		}
		if(accept.getGh_printTime()!=null)
		{
			//交易票
			view.setBs6("1");
		}
		if(accept.getZy_printTime()!=null&&accept.getLicenseAccepTimeS()==null&&accept.getStatus()!=16)
		{
			view.setBs7("1");
		}
		TransferFinanceInfo finanace = new TransferFinanceInfo();
		finanace = finanace.findFinanceInfoByserial(accept.getSerial());
		if(accept.getChangeTradeTicket()==2&&accept.getGh_printTime()!=null&&finanace.getChangePriceDates()==null)
		{
			view.setBs8("1");
		}
		
		/***
		 * 打印部分数据
		 */
		
		
		
		request.setAttribute(TmsConstants.VIEWHELP,view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private TransferRgInputViewHelper toViewHelper(Car entity,TransferRgInputViewHelper view) throws TmsDaoException {

		Codes code = Codes.getInstance();
		view.setCarName(entity.getCarName());
		view.setAttribute(code.getItemValueByItemIdAndItemCode(Codes.ATTACHMENT_PROPERTY, entity.getAttribute()));
		view.setBrandId(entity.getBrandId());
		view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, entity.getCardType()));
		view.setLicensePlate(entity.getLicensePlate());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setCarType(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, entity.getCarType()));
		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE, entity.getCarUse()));
		view.setColor(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR, entity.getColor()));
		view.setCountryStyle(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY, entity.getCountryStyle()));
		if(entity.getDisplacement()>0)
		{
			view.setDisplacement(String.valueOf(entity.getDisplacement()));
		}else{
			view.setDisplacement("");
		}
		
		view.setEngine(entity.getEngine());
		view.setFuelType(code.getItemValueByItemIdAndItemCode(Codes.FUEL_TYPE, entity.getFuelType()));
		view.setGearbox(code.getItemValueByItemIdAndItemCode(Codes.GEARBOX_TYPE, entity.getGearbox()));
		view.setJqx(entity.getJqx());
		if(entity.getLoadquality() > 0)
		{
			view.setLoadquality(String.valueOf(entity.getLoadquality()));
		}else{
			view.setLoadquality("");
		}
		view.setBrandId(entity.getBrandId());
		view.setVendorId(entity.getVendorId());		
		view.setCarStyleId(entity.getCarStyleId());
		if(entity.getMileage() > 0)
		{
			view.setMileage(StringUtil.addDH(String.valueOf(entity.getMileage())));
		}else{
			view.setMileage("");
		}
		view.setModelCode(entity.getModelCode());
		view.setOwnerAddress(entity.getOwnerAddress());
		view.setOwnerOrgNo(entity.getOwnerOrgNo());
		view.setOwnerPhone(entity.getOwnerPhone());
		view.setPassenger(String.valueOf(entity.getPassenger()));
		view.setPrice(StringUtil.addDH(String.valueOf(entity.getPrice())));

		view.setRegDate(entity.getRegDate());
		view.setRegisterCardNo(entity.getRegisterCardNo());
		view.setSendCardDate(entity.getSendCardDate());
//		view.setSerial(entity.getSerial());
		
		view.setVendorCode(entity.getVendorCode());
//		view.setVin(entity.getVin());
		view.setYearCarefulPeriod(entity.getYearCarefulPeriod());
		
		return view;

	}
	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private TransferRgInputViewHelper toViewHelper1(TransferPersonalCarInfo entity,TransferRgInputViewHelper view) throws TmsDaoException {

		Codes code = Codes.getInstance();
		view.setCarName(entity.getCarName());
		view.setAttribute(code.getItemValueByItemIdAndItemCode(Codes.ATTACHMENT_PROPERTY, entity.getAttribute()));
		view.setBrandId(entity.getBrandId());
		view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, entity.getCardType()));
		view.setLicensePlate(entity.getLicensePlate());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setCarType(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, entity.getCarType()));
		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE, entity.getCarUse()));
		view.setColor(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR, entity.getColor()));
		view.setCountryStyle(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY, entity.getCountryStyle()));
		view.setDisplacement(String.valueOf(entity.getDisplacement()));
		view.setEngine(entity.getEngine());
		view.setFuelType(code.getItemValueByItemIdAndItemCode(Codes.FUEL_TYPE, entity.getFuelType()));
		view.setGearbox(code.getItemValueByItemIdAndItemCode(Codes.GEARBOX_TYPE, entity.getGearbox()));
		view.setJqx(entity.getJqx());
		if(entity.getLoadquality() > 0)
		{
			view.setLoadquality(String.valueOf(entity.getLoadquality()));
		}else{
			view.setLoadquality("");
		}
		view.setBrandId(entity.getBrandId());
		view.setVendorId(entity.getVendorId());		
		view.setCarStyleId(entity.getCarStyleId());
		if(entity.getMileage() > 0)
		{
			view.setMileage(StringUtil.addDH(String.valueOf(entity.getMileage())));
		}else{
			view.setMileage("");
		}
		view.setModelCode(entity.getModelCode());
		view.setOwnerAddress(entity.getOwnerAddress());
		view.setOwnerOrgNo(entity.getOwnerOrgNo());
		view.setOwnerPhone(entity.getOwnerPhone());
		if(entity.getPassenger()>0)
		{
			view.setPassenger(String.valueOf(entity.getPassenger()));
		}else{
			view.setPassenger("");
		}
		
		view.setPrice(StringUtil.addDH(String.valueOf(entity.getPrice())));

		view.setRegDate(entity.getRegDate());
		view.setRegisterCardNo(entity.getRegisterCardNo());
		view.setSendCardDate(entity.getSendCardDate());
//		view.setSerial(entity.getSerial());
		
		view.setVendorCode(entity.getVendorCode());
//		view.setVin(entity.getVin());
		view.setYearCarefulPeriod(entity.getYearCarefulPeriod());
		
		return view;
	}	
}
